﻿using System;
using System.Collections.Generic;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using MyFramework.Common;
using MyFramework.Common.Interfaces;

namespace MyFramework.Core
{
    /// <summary>
    /// Unity controller factory
    /// </summary>
    public class UnityControllerFactory : ControllerFactoryBase
    {
        #region Fields

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="UnityControllerFactory"/> class.
        /// </summary>
        public UnityControllerFactory()
        {

        }

        /// <summary>
        /// Creates the controller instance.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="initParameters">The initialize parameters.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        protected override IController CreateControllerInstance(Type type, IDictionary<string, object> initParameters)
        {
            var container = ServiceLocator.Current.GetInstance<IUnityContainer>();
            IController controller = Activator.CreateInstance(type) as IController;
            container.RegisterInstance<IController>(type.Name, controller);
            return container.Resolve<IController>(type.Name);
        }
    }
}
